<template>
  <div>
    <van-cell-group>
      <van-cell>
        <div
          slot="title"
          class="header"
        >
          <van-image
            round
            :src="data.photo"
          />
          <div class="message">
            <div class="name">{{data.name}}</div>
            <div class="fans">粉丝数：{{data.fans_count}}</div>
          </div>
        </div>
        <van-button
          @click="deleteUser"
          size="mini"
          plain
          v-if="data.mutual_follow"
        >互相关注</van-button>
        <van-button
          @click="deleteUser"
          size="mini"
          plain
          v-else-if="i===0"
        >已关注</van-button>
        <van-button
          @click="handle"
          size="mini"
          v-else
          type="danger"
        >关注</van-button>
      </van-cell>
    </van-cell-group>
  </div>
</template>
<script>
import dayjs from 'dayjs'
import { followedUser, deleteUser } from '@/api/user.js'
export default {
  props: {
    data: {
      type: [Object],
      required: true
    },
    active: {
      type: [String, Number],
      required: true
    }
  },
  computed: {
    date: function () {
      return dayjs(this.data.pubdate).format('YYYY-MM-DD hh:mm')
    }
  },
  data () {
    return {
      i: this.active
    }
  },
  methods: {
    async  handle () {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      try {
        await followedUser(this.data.id)
        this.$toast.success('关注成功')
        if (this.i === 1) {
          this.data.mutual_follow = true
        } else {
          this.i = 0
        }
      } catch (err) {
        this.$toast.fail('操作失败')
      }
    },
    async  deleteUser () {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      try {
        await deleteUser(this.data.id)
        this.$toast.success('取消关注成功')
        if (this.data.mutual_follow === true) {
          this.data.mutual_follow = false
          this.i = 1
        } else {
          this.i = 2
        }
      } catch (err) {
        this.$toast.fail('操作失败')
      }
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .header {
  display: flex;
  .van-image {
    width: 72px;
    height: 72px;
    margin-right: 16px;
  }
  .message {
    .name {
      margin-top: 2px;
      font-size: 25px;
      color: #222222;
      height: 35px;
      line-height: 35px;
    }
    .time {
      font-size: 17px;
      color: #b0b0b0;
      height: 35px;
      line-height: 35px;
    }
  }
}
</style>
